<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Code by TinhNQ
 * 2014
 **/
class Restaurant extends Admin_Controller {

    function __construct(){
        parent::__construct();
        $this->admin_navigation->parent_active('restaurant');
        $this->load->library('session');
        $this->output->enable_profiler(TRUE);
        $this->load->model('restaurant/restaurant_model');
    }

    function manager_restaurant() {
        $columns = array(
                array(
                    'name' => 'restaurant_id',
                    'index' => 'restaurant_id',
                    'sort_column' => 'restaurant_id',
                ),
                array(
                    'name' => 'name',
                    'type' => 'text',
                    'index' => 'name',
                    'filter' => 'name',
                    'sort_column' => 'name',
                ),
                array(
                    'name' => 'is_hot',
                    'type' => 'publish',
                    'index' => 'is_hot',
                    'filter' => 'is_hot',
                    'options' => array('0' => 'Không', '1' => 'Có'),
                ),
                array(
                    'name' => 'featured',
                    'type' => 'publish',
                    'index' => 'featured',
                    'filter' => 'featured',
                    'options' => array('0' => 'Không', '1' => 'Có'),
                ),
                array(
                    'name' => 'created',
                    'type' => 'date',
                    'index' => 'created',
                ),

            );
        $this->grid->columns($columns);
        $this->grid->datasource('restaurant/restaurant_model','get_restaurants');
        $this->grid->set_title('Restaurant Manager');
        $this->grid->add_button('Thêm mới',admin_url('*/restaurant/add_restaurant'),'add');
        $this->grid->set_row_action('edit_restaurant');
        $this->grid->total_rows($this->restaurant_model->get_count());
        $this->grid->add_mass_action('delete', array(
                'label'=>'Xóa',
                'url'      => admin_url('*/restaurant/delete_restaurant'),
                'confirm'  => 'Are you sure?'
        ));
        $this->grid->add_mass_action('status', array(
                'label'=>'Thay đổi trạng thái',
                'url'      => admin_url('*/restaurant/change_status_restaurant'),
        ));
        if ($this->input->is_ajax()){
            $this->output->set_output($this->grid->display());
        }else{
            $this->load->view('admin/grid');
        }
    }

    function edit_restaurant() {
        $restaurant_id = $this->input->get_param('id', false);
        $this->load->model('restaurant/restaurant_model');
        if ($restaurant_id) {
            $data = $this->restaurant_model->get_restaurant($restaurant_id);
            $this->form->set_values($data);
        }
        $this->form->set_form_attributes(array(
                'id' => 'restaurant_form',
                'action' => admin_url('*/restaurant/edit_post_restaurant' , array('id' => $restaurant_id)),
                'enctype' => 'multipart/form-data',
                ));
        $this->form->add_filedset('ganeral', array('legend' => 'General'));
        $this->form->add_filed('name', 'text', array('label' => 'name', 'name' => 'name', 'required' => true,));
        $this->form->add_filed('alias', 'text', array('label' => 'alias', 'name' => 'alias', ));
        $this->form->add_filed('introtext', 'textarea', array('label' => 'introtext', 'name' => 'introtext', ));
        $this->form->add_filed('fulltext', 'editor', array('label' => 'fulltext', 'name' => 'fulltext', ));
        $this->form->add_filed('image1', 'text', array('label' => 'image1', 'name' => 'image1', ));
        $this->form->add_filed('image2', 'text', array('label' => 'image2', 'name' => 'image2', ));
        $this->form->add_filed('image3', 'text', array('label' => 'image3', 'name' => 'image3', ));
        $this->form->add_filed('image4', 'text', array('label' => 'image4', 'name' => 'image4', ));
        $this->form->add_filed('image5', 'text', array('label' => 'image5', 'name' => 'image5', ));
        $this->form->add_filed('is_hot', 'publish', array('label' => 'is_hot', 'name' => 'is_hot', ));
        $this->form->add_filed('featured', 'publish', array('label' => 'featured', 'name' => 'featured', ));
        $this->form->add_filed('region_id', 'select', array('label' => 'region_id', 'name' => 'region_id', ));
        $this->form->add_filed('pref_id', 'select', array('label' => 'pref_id', 'name' => 'pref_id', ));
        $this->form->add_filed('city_id', 'select', array('label' => 'city_id', 'name' => 'city_id', ));
        $this->form->add_filed('address', 'text', array('label' => 'address', 'name' => 'address', ));
        $this->form->add_filed('localtion', 'text', array('label' => 'localtion', 'name' => 'localtion', ));
        $this->form->add_filed('tel', 'text', array('label' => 'tel', 'name' => 'tel', ));
        $this->form->add_filed('url', 'text', array('label' => 'url', 'name' => 'url', ));
        $this->form->add_filed('status', 'publish', array('label' => 'status', 'name' => 'status', ));
        $this->form->add_filed('ordering', 'select', array('label' => 'ordering', 'name' => 'ordering', ));
        $this->form->add_filed('create_by', 'select', array('label' => 'create_by', 'name' => 'create_by', ));
        $this->form->add_filed('created', 'date', array('label' => 'created', 'name' => 'created', ));
        $this->form->add_filed('update_by', 'select', array('label' => 'update_by', 'name' => 'update_by', ));
        $this->form->add_filed('update', 'date', array('label' => 'update', 'name' => 'update', ));

        $this->load->view('admin/form', array('form' => $this->form->to_html()));
    }

    function add_restaurant() {
        $this->_forward('edit_restaurant');
    }

    function delete_restaurant() {
        $cids = explode(',', $this->input->get_param('new_id',false));
        $this->load->model('restaurant/restaurant_model');
        foreach ($cids as $cid) {
            $this->restaurant_model->delete_restaurant($cid);
        }
        $this->_redirect(admin_url('*/restaurant/manager_restaurant'));
    }

    function change_status_restaurant() {
        $cids = explode(',', $this->input->get_param('new_id',false));
        $this->load->model('restaurant/restaurant_model');
        foreach ($cids as $cid) {
            $this->restaurant_model->change_status_restaurant($cid);
        }
        $this->_redirect(admin_url('*/restaurant/manager_restaurant'));
    }

    function edit_post_restaurant() {
        $restaurant_id = $this->input->get_param('id', false);
        $this->load->model('restaurant/restaurant_model');
        if (($data = $this->input->post())) {
            $config['upload_path'] = './images/content/';
            $config['allowed_types'] = 'gif|jpg|png';
            $this->load->library('upload', $config);

            if ($this->upload->do_upload('image')) {
                $data['image'] = 'images/content/' . $this->upload->get_file_name();
            }
            if ($restaurant_id) {
                $this->restaurant_model->update_restaurant($data, $restaurant_id);
            } else {
                $this->restaurant_model->insert_restaurant($data);
            }
            $this->_redirect(admin_url('*/restaurant/manager_restaurant'));
        }
    }
}
?>